package cn.woo5.wechat.open.study.util;

import java.nio.charset.Charset;
import java.util.Arrays;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class CipherUtil {

  final static Charset CHARSET_UTF8 = Charset.forName("UTF-8");

  public boolean weixinSignCheck(String sign, String... args) {
    boolean result = false;
    String calcSign = this.weixinSha1Sign(args);
    if (sign != null && sign.equals(calcSign)) {
      result = true;
    }
    return result;
  }

  public String weixinSha1Sign(String... args) {
    if (args == null) {
      return null;
    }
    Arrays.sort(args);
    String combine = String.join("", args);
    return this.sha1Encript(combine);
  }

  public String sha1Encript(String src) {
    log.debug(src);
    if (src == null) {
      return null;
    }
    String result = DigestUtils.sha1Hex(src);
    log.debug(src, result);
    return result;
  }
}
